package com.example.common.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import android.widget.RadioGroup
import com.example.common.R

class GridRadioGroup: RadioGroup {

    companion object{
        private const val VERTICAL_SPACING_DIP = 15

        private const val HORIZONTAL_SPACING_DIP = 10
    }

    private var verticalSpacing = 20f

    private var horizontalSpacing = 12f

    private var numColumns = 3

    constructor(context: Context):this(context, null){

    }

    constructor(context: Context, attrs: AttributeSet?):super(context, attrs){
        val attributes: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.GridRadioGroup)

        numColumns = attributes.getInt(R.styleable.GridRadioGroup_numColumns, numColumns)

        val tempHorSpacing = (HORIZONTAL_SPACING_DIP * context.resources.displayMetrics.density)
        horizontalSpacing = attributes.getDimension(R.styleable.GridRadioGroup_horizontalSpacing, tempHorSpacing)

        val tempVerSpacing = (VERTICAL_SPACING_DIP * context.resources.displayMetrics.density)
        verticalSpacing = attributes.getDimension(R.styleable.GridRadioGroup_verticalSpacing, tempVerSpacing)

        attributes.recycle()
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        val childCount = childCount
        val contentWidth = r - l - paddingRight - paddingLeft
        val itemWidth = (contentWidth - horizontalSpacing * (numColumns - 1)).toInt() / numColumns

        var x = paddingLeft // 横坐标开始

        var y = 0 //纵坐标开始

        var rows = 0
        for (i in 0 until childCount) {
            val view: View = getChildAt(i)
            val height: Int = view.getMeasuredHeight()
            x += itemWidth
            if (i % numColumns == 0) {
                x = paddingLeft + itemWidth
                rows++
            }
            y = rows * height + (rows - 1) * verticalSpacing.toInt() + paddingTop
            view.layout(x - itemWidth, y - height, x, y)
            x += horizontalSpacing.toInt()
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val childCount = childCount
        val specWidth = MeasureSpec.getSize(widthMeasureSpec)
        val contentWidth = specWidth - paddingRight - paddingLeft
        val itemWidth = (contentWidth - horizontalSpacing * (numColumns - 1)).toInt() / numColumns

        var y = 0 //纵坐标开始

        var rows = 0
        for (i in 0 until childCount) {
            val child = getChildAt(i)
            child.measure(MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY), MeasureSpec.UNSPECIFIED)
            val height = child.measuredHeight
            if (i % numColumns == 0) {
                rows++
            }
            y = rows * height + (rows - 1) * verticalSpacing.toInt() + paddingTop + paddingBottom
        }

        setMeasuredDimension(specWidth, y)
    }
}

